// shopPages/shoplist/shoplist.js
var sliderWidth = 70;
var app = getApp();
var selectedNews = false;
var newsList = [];
// var page = 1;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    s_list: [],
    s_list1: [],
    s_list2: [],
    s_list3: [],

    // 选项卡
    tabs: ["综合", "价格", "抵扣", "销量"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    // goodsParam:[],
    page: 1,
    hui0_show: true,
    hui1_show: true,
    hui2_show: true,
    top0_show: false,
    top1_show: false,
    top2_show: false,
    currentSelectTripType: 'one',
  },
  selectedZH: function(e) {
    var that = this;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui0_show: true,
      hui1_show: true,
      hui2_show: true,
    })
    var goodsParam = that.data.goodsParam;
    goodsParam.sort = '';
    that.setData({
      goodsParam: goodsParam,
      page: 1
    })
    newsList = [];
    that.getGoodsList(goodsParam, 1);
  },
  selectedJG: function(e) {
    var that = this
    var goodsParam = that.data.goodsParam;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui0_show: false,
      hui1_show: true,
      hui2_show: true,
    })
    if (that.data.top0_show) {
      goodsParam.sort = 'price desc';
      that.setData({
        top0_show: false,
        goodsParam: goodsParam,
        page: 1
      })
    } else {

      goodsParam.sort = 'price asc';
      that.setData({
        top0_show: true,
        goodsParam: goodsParam,
        page: 1
      })
    }
    newsList = [];
    that.getGoodsList(goodsParam, 1);

  },
  selectedDK: function(e) {
    var that = this
    var goodsParam = that.data.goodsParam;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui1_show: false,
      hui0_show: true,
      hui2_show: true
    })
    if (that.data.top1_show) {
      goodsParam.sort = 'integral_price desc';
      that.setData({
        top1_show: false,
        goodsParam: goodsParam,
        page: 1
      })
    } else {
      goodsParam.sort = 'integral_price asc';
      that.setData({
        top1_show: true,
        goodsParam: goodsParam,
        page: 1
      })
    }
    newsList = [];
    that.getGoodsList(goodsParam, 1);
  },
  selectedXL: function(e) {
    var that = this
    var goodsParam = that.data.goodsParam;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui2_show: false,
      hui1_show: true,
      hui0_show: true
    })
    if (that.data.top2_show) {
      goodsParam.sort = 'mount desc';
      that.setData({
        top2_show: false,
        goodsParam: goodsParam,
        page: 1
      })
    } else {
      goodsParam.sort = 'mount asc';
      that.setData({
        top2_show: true,
        goodsParam: goodsParam,
        page: 1
      })
    }
    newsList = [];
    that.getGoodsList(goodsParam, 1);
  },

  bindshop: function(event) {
    var classify = event.currentTarget.dataset.classify;
    var index = event.currentTarget.dataset.id;
    var that = this;

    var hui_show = that.data.hui_show;

    // console.log(top_show);
    // console.log(hui_show)
    console.log(that.data.top_show_price)
    if (index == 1) {
      var top_show_price = that.data.top_show_price;
      if (top_show_price) {
        that.setData({
          top_show_price: false,
          bottom_show_price: true,
        });
      } else {
        that.setData({
          top_show_price: true,
          bottom_show_price: false,
        });
      }
    } else if (index == 2) {
      var top_show_integral = that.data.top_show_integral;
      if (top_show_integral) {
        that.setData({
          top_show_integral: false,
          bottom_show_integral: false,
        });
      } else {
        that.setData({
          top_show_integral: false,
          bottom_show_integral: true,
        });
      }
    } else if (index == 3) {
      var bottom_show_sale_num = that.data.bottom_show_sale_num;
      if (bottom_show_sale_num) {
        that.setData({
          bottom_show_sale_num: false,
          bottom_show_sale_num: false,
        });
      } else {
        that.setData({
          bottom_show_sale_num: false,
          bottom_show_sale_num: true,
        });
      }
    }



    console.log(classify) //输出的结果就是你点击的
    this.setData({
      shopitem: classify, //更新

    })
  },

  bindlist: function(e) {
    wx.navigateTo({
      url: '',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var goodsParam = {
      sort: ''
    };
    if (options.p_cat_id != undefined) {
      // var cat_id = options.cat_id;
      goodsParam.p_cat_id = options.p_cat_id;
    }
    if (options.is_integral != undefined) {
      // var cat_id = options.cat_id;
      goodsParam.is_integral = options.is_integral;
    }
    if (options.cat_id != undefined) {
      // var cat_id = options.cat_id;
      goodsParam.cat_id = options.cat_id;
    }
    if (options.is_new != undefined) {
      // var is_new = options.is_new;
      goodsParam.is_new = options.is_new;
    }
    if (options.is_hot != undefined) {
      // var is_hot = options.is_hot;
      goodsParam.is_hot = options.is_hot;
    }
    if (options.keywords != undefined) {
      // var keywords = options.keywords;
      goodsParam.keywords = options.keywords;
    }
    if (options.is_jinlie_hot != undefined) {
      // var keywords = options.keywords;
      goodsParam.is_jinlie_hot = options.is_jinlie_hot;
    }
    if (options.is_jinlie_rec != undefined) {
      // var keywords = options.keywords;
      goodsParam.is_jinlie_rec = options.is_jinlie_rec;
    }
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    that.setData({
      goodsParam: goodsParam
    });
    selectedNews = false;
    newsList = [];

    that.getGoodsList(goodsParam, that.data.page);
  },
  //获取商品列表
  getGoodsList: function(param, pageno) {
    var that = this;
    var newsList = that.data.goodsList;
    var url2 = '/mall/index';
    param.pageno = pageno;
    param.pagesize = 10;
    wx.showLoading({
      title: '玩命加载中',
    })
    // console.log(param);return false;
    app.HttpPostSend(url2, param, function(res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          goodsList: []
        })
      } else {
        var page = that.data.page;
        if (page == 1) {
          newsList = [];
        } else {

        }
        var goodsList = res.data;
        for (let i = 0; i < goodsList.length; i++) {
          newsList.push(goodsList[i]);
        }
        if (goodsList.length < 10) {
          selectedNews = false;
        } else {
          selectedNews = true;
          var page = that.data.page;
          page += 1;
          that.setData({
            page: page
          });
        }
        that.setData({
          goodsList: newsList
        })

      }
      wx.hideLoading();
    });
  },
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  onReachBottom: function() {

    if (selectedNews == true) {
      var that = this;
      // 显示加载图标
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1
      that.getGoodsList(that.data.goodsParam, that.data.page);

    } else {
      wx.hideLoading();
    }
  },
  toGoodsDetail_: function(e) {
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id=" + e.currentTarget.dataset.id
    })
  }
})